package com.example.treasure_back.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.treasure_back.mapper.RelationMapper;
import com.example.treasure_back.mapper.UserMapper;
import com.example.treasure_back.model.RelationModel;
import com.example.treasure_back.model.UserModel;
import com.example.treasure_back.model.vo.FriendModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MessageService {
    @Autowired
    private RelationMapper relationMapper;
    @Autowired
    private UserMapper userMapper;

    public List<FriendModel> getFriendList(String userId){
        QueryWrapper<RelationModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<RelationModel> relationModels = relationMapper.selectList(queryWrapper);
        ArrayList<FriendModel> list = new ArrayList<>();
        for (RelationModel relationModel : relationModels) {
            UserModel userModel = userMapper.selectById(relationModel.getFriendId());
            FriendModel friendModel = new FriendModel();
            friendModel.setEmail(userModel.getEmail());
            friendModel.setImage(userModel.getAvatar());
            friendModel.setUsername(userModel.getUsername());
            list.add(friendModel);
        }
        return list;
    }
}
